package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.itheima.reggie.common.R;
import com.itheima.reggie.common.SessionThreadLocal;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 加入购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R add(@RequestBody ShoppingCart shoppingCart){

        log.info("添加物品..........");

        //将此时的用户id赋值给购物车 便于查询
        shoppingCart.setUserId(SessionThreadLocal.getThreadLocal());
        //设置此时的下单时间
        shoppingCart.setCreateTime(LocalDateTime.now());

        //组装查询购物车信息
        LambdaQueryWrapper<ShoppingCart> shoppingCartWrapper = new LambdaQueryWrapper<>();
        shoppingCartWrapper.eq(shoppingCart.getDishId() != null,ShoppingCart::getDishId,shoppingCart.getDishId());
        shoppingCartWrapper.eq(shoppingCart.getSetmealId() != null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId());

        //查询购物车是否包含该物品
        ShoppingCart one = shoppingCartService.getOne(shoppingCartWrapper);

        //不存在时 就直接保存该物品
        if (one == null){
            shoppingCart.setNumber(1);
            shoppingCartService.save(shoppingCart);
        }else {
            //存在就将其number+1
            one.setNumber(one.getNumber() + 1);
            //并修改其数据库
            shoppingCartService.updateById(one);
            //把最新的商品信息赋给传过来的商品信息 方便前端获取 改变其数字
            shoppingCart = one;
        }
        //返回商品 方便前端获取 改变其数字 获取最新的number
        return R.success(shoppingCart);
    }

    /**
     * 查询购物车 所有商品
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        //查看当前用户的所有商品
        LambdaQueryWrapper<ShoppingCart> shoppingCartWrapper = new LambdaQueryWrapper<>();
        shoppingCartWrapper.eq(ShoppingCart::getUserId,SessionThreadLocal.getThreadLocal());

        List<ShoppingCart> shoppingCarts = shoppingCartService.list(shoppingCartWrapper);

        return R.success(shoppingCarts);
    }

    /**
     * 删除商品
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        //组装条件 判断是菜品还是套餐
        LambdaQueryWrapper<ShoppingCart> shoppingCartWrapper = new LambdaQueryWrapper<>();
        shoppingCartWrapper.eq(shoppingCart.getDishId() != null,ShoppingCart::getDishId,shoppingCart.getDishId());
        shoppingCartWrapper.eq(shoppingCart.getSetmealId() != null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        //获取该商品信息
        ShoppingCart one = shoppingCartService.getOne(shoppingCartWrapper);

        //判断此时的商品数量 是为1的话，再减少就是 直接删除
        if (one != null && one.getNumber() == 1){
            shoppingCartService.removeById(one);
        }else {
            //不为1 说明还可以减少 直接number-1
            one.setNumber(one.getNumber() - 1);
            //并修改其数据
            shoppingCartService.updateById(one);
            //同样需要赋予最新信息 方便前端来获取
            shoppingCart = one;
        }
        //返回最新值 获取最新的number
        return R.success(one);
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R clean(){
        //直接清空该用户下所有物品
        LambdaQueryWrapper<ShoppingCart> shoppingCartWrapper = new LambdaQueryWrapper<>();
        shoppingCartWrapper.eq(ShoppingCart::getUserId,SessionThreadLocal.getThreadLocal());

        shoppingCartService.remove(shoppingCartWrapper);
        return R.success();
    }

}
